<?php

class Default_Form_Login extends Zend_Form {
	
	const PASSWORD_MIN_LENGTH    = 6;
	const PASSWORD_MAX_LENGTH    = 32;
	const ENCODING       		 = 'UTF-8';
	const USERNAME_LABEL 	     = "Tên đăng nhập";
	const PASSWORD_LABEL         = "Mật khẩu";
	
	public function init() {
		
		$this	->setName('loginform')
				->setMethod('post');
		//User name
		$username = self::addUsername();
		//Password
		$password = self::addPassword();

		$remember = new Zend_Form_Element_Checkbox('login_remember');
		$remember->setDecorators(array('ViewHelper'));
		$this->addElements(array($username, $password, $remember));
	}

	/*
	 * Add username element
	 */
	protected  function addUsername() {
		//Alpha validate
        //Not empty
        $usernameNotEmpty = new Zend_Validate_NotEmpty();
        $tb_name = new Admin_Model_DbTable_Users();
        $username = new Zend_Form_Element_Text('login_username');
        $username	->setLabel(self::USERNAME_LABEL)
        			->addFilter('StripTags')
        			->setAttrib('placeholder', 'Tên đăng nhập')
        			->addFilter('StringTrim')
        			->setRequired(true)
        			->addValidator($usernameNotEmpty,true)
        			->addValidator('regex', true, 
                       array(
                           'pattern'=>'/^[a-zA-Z0-9\@\.\_]+$/', 
                           'messages'=>array(
                               'regexNotMatch'=>self::USERNAME_LABEL." chứa ký tự không hợp lệ."                           )
                       ))
        			
        		->setDecorators(array('ViewHelper'));	
		return $username;
	}

	/*
	 * Add password element
	 */
	protected function addPassword() {
		//Not empty validate
		$passwordNotEmpty = new Zend_Validate_NotEmpty();
        //Length validate
        $passwordLength = new Zend_Validate_StringLength(self::PASSWORD_MIN_LENGTH, self::PASSWORD_MAX_LENGTH, self::ENCODING);
		 $password = new Zend_Form_Element_Password('login_password');
         $password	->setLabel(self::PASSWORD_LABEL)
         			->setAttrib('placeholder', 'Mật khẩu')
         			->addFilter('StripTags')
         			->addFilter('StringTrim')
         			->setRequired(true)
         			->addValidator($passwordNotEmpty,true)
         			->addValidator($passwordLength)
         			->setDecorators(array('ViewHelper'));
         return $password;
	}
}